package cn.yudao.ymcc.controller;

import cn.yudao.ymcc.dto.CourseEalsticSearchDto;
import cn.yudao.ymcc.result.JSONResult;
import cn.yudao.ymcc.result.PageList;
import cn.yudao.ymcc.searchDoc.CourseDoc;
import cn.yudao.ymcc.service.IElasticSearchService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
public class ElasticSearchController {

    @Autowired
    private IElasticSearchService elasticSearchService;

    /**
     * @Title: save
     * @Description: 添加到ES
     * @Author: Mr.Jia
     * @Version: 1.0
     * @Date:  2022/8/15 10:52
     * @Parameters:
     * @Return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('es:saveCourse')")
    public JSONResult save(@RequestBody CourseDoc courseDoc){
        elasticSearchService.save(courseDoc);
        return JSONResult.success();
    }

    // 根据条件查询ES
    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseEalsticSearchDto dto){
        PageList<CourseDoc> page1 = elasticSearchService.page(dto);
        return JSONResult.success(page1);
    }

    /**
     * @Title: delete
     * @Description: 从ES中删除
     * @Author: Mr.Jia
     * @Version: 1.0
     * @Date:  2022/8/15 10:53
     * @Parameters:
     * @Return
     */
    @PostMapping("/delete")
    public JSONResult delete(@RequestBody Long id){
        elasticSearchService.delete(id);
        return JSONResult.success();
    }

}
